<template>
  <el-dialog
    :title="dialog.title"
    :visible.sync="dialog.show"
    width="600px"
    custom-class="mydialog"
    append-to-body
    @close="handleClose"
  >
    <div class="dialog-content">
      <el-form ref="form1" :model="form" :rules="rules" label-width="150px">
        <el-form-item
          :rules="[{required: true, message: '盘点仓库不能为空', trigger: 'blur' }]"
          label="盘点仓"
        >
          <el-select v-model="warehouseId" placeholder="请选择仓库" style="width: 250px;" filterable>
            <el-option
              v-for="warehouse in warehouseList"
              :label="warehouse.warehouseName"
              :value="warehouse.warehouseId"
              :key="warehouse.warehouseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :rules="[{required: true, message: '盘点范围不能为空', trigger: 'blur' }]"
          label="盘点范围"
        >
          <el-select v-model="inventoryType" placeholder="请选择范围" style="width: 250px;" filterable>
            <el-option
              v-for="inventoryType in inventoryTypeList"
              :label="inventoryType.name"
              :value="inventoryType.id"
              :key="inventoryType.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="remark"/>
        </el-form-item>
      </el-form>
      <div class="text-center mt-30">
        <el-button class="greenbt" type="primary" @click="addInventorySheet">保存</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { addInventorySheet } from '@/api/pas/inventory'
import { queryList } from '@/api/pas/warehouse'
export default {
  props: {
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      listLoading: true,
      remark: null,
      inventoryType: null,
      warehouseId: null,
      inventoryTypeList: [
        { id: 1, name: '整机' },
        { id: 2, name: '辅材' },
        { id: 3, name: '工具' },
        { id: 4, name: '赠品' },
        { id: 99, name: '全部' }
      ],
      warehouseList: [],
      dialog: {
        title: '',
        show: true
      },
      form: {
        type_options: [],
        type: '',
        number: ''
      },
      rules: {},
      tableData: []
    }
  },
  created() {
    this.queryAllWarehouse()
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    addInventorySheet: function() {
      this.listLoading = true
      if (!this.warehouseId) {
        this.$message({
          message: '请选择盘点仓库',
          type: 'warning'
        })
        return
      }
      if (!this.inventoryType) {
        this.$message({
          message: '请选择盘点范围',
          type: 'warning'
        })
        return
      }
      var data = {
        warehouseId: this.warehouseId,
        inventoryType: this.inventoryType,
        remark: this.remark
      }
      addInventorySheet(data)
        .then(response => {
          if (response.data.status === 'success') {
            this.$message({
              message: '添加盘点单成功',
              type: 'success'
            })
            this.listLoading = false
            this.handleClose()
          } else {
            this.$message({
              message: '添加盘点失败,' + response.data.messge,
              type: 'warning'
            })
            this.listLoading = false
          }
        })
        .catch(error => {
          this.$message({
            message: '添加盘点失败',
            type: 'warning'
          })
          this.listLoading = false
          console.log(error)
        })
    },
    queryAllWarehouse() {
      var data = {
        deteleFlag: 'N',
        isRange: ''
      }
      queryList(data)
        .then(response => {
          if (response.data.status === 'success') {
            this.warehouseList = response.data.data.list
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}
</script>
